package com.css.fxfzaqbz.modules.data.repository.impl;


import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.entity.SeismicHazardAssess;
import com.css.fxfzaqbz.modules.data.queryParams.ImportantActivityQueryParams;
import com.css.fxfzaqbz.modules.data.queryParams.SeismicHazardAssessQueryParams;
import com.css.fxfzaqbz.modules.data.repository.ImportantActivityRepository;
import com.css.fxfzaqbz.modules.data.repository.SeismicHazardAssessRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SeismicHazardAssessRepositoryImpl implements SeismicHazardAssessRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public Map<String, Object> getAllSeismicHazardAssessParams(SeismicHazardAssessQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select aa.* from (\n" +
//                "\tSELECT i.*,b.bs_status,b.content,b.bs_id as bb_id FROM sys_big_seconddisasters b\n" +
//                "\tleft JOIN sys_important_activity i ON i.ia_id = b.bs_activeid\n" +
//                "\tunion all\n" +
                "\tselect i.* ,bc.bc_status bs_status,bc.content,bc.bc_id as bb_id FROM sys_big_casualties bc  left JOIN sys_important_activity i ON i.ia_id = bc.bc_activeid\n" +
                "\tunion all\n" +
                "\tselect i.* ,bd.bd_status bs_status,bd.content,bd.bd_id as bb_id FROM sys_big_disaster bd left JOIN sys_important_activity i ON i.ia_id = bd.bd_activeid\n" +
                "\tunion all\n" +
                "\tselect i.* ,ee.ee_status bs_status,ee.content,ee.ee_id as bb_id FROM sys_earth_assess ee left JOIN sys_important_activity i ON i.ia_id = ee.ee_activeid\n" +
//                "\tunion all\n" +
//                "\tselect i.* ,ut.ut_status,ut.content,ut.ut_id as bb_id FROM sys_u_task ut left JOIN sys_important_activity i ON i.ia_id = ut.ut_activeid\n" +
                "\tunion all\n" +
                "\tselect i.* ,kt.kt_status bs_status,kt.content,kt.kt_id as bb_id FROM sys_kr_task kt left JOIN sys_important_activity i ON i.ia_id = kt.kt_activeid) aa");


        StringBuilder orderSql = new StringBuilder(" order by aa.create_time desc , aa.content asc");

//        if (PlatformObjectUtils.isEmpty(queryParams.getIaActivityStatus())) {
//            queryParams.setIaActivityStatus(YNEnum.N.toString());
//        }
        StringBuilder whereSql = new StringBuilder(" where aa.del_flag = '" + YNEnum.N.toString() + "'");


        if (!PlatformObjectUtils.isEmpty(queryParams.getProvinceId())&&queryParams.getProvinceId()!="") {
            whereSql.append(" and aa.province_id = '"+queryParams.getProvinceId()+"'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDlrId())) {
            whereSql.append(" and aa.create_user  ='" + queryParams.getDlrId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getIaNumber())) {
            whereSql.append(" and aa.ia_number like '%" + queryParams.getIaNumber() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getIaName())) {
            whereSql.append(" and aa.ia_name like '%" + queryParams.getIaName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getBsStatus())){
            whereSql.append("and bs_status = '"+queryParams.getBsStatus()+"'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getIaActivityStatus())){
            whereSql.append("and ia_activity_status ='"+queryParams.getIaActivityStatus()+"'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getContent())){
            whereSql.append("and content ='"+queryParams.getContent()+"'");
        }
       /* if (queryParams.getStatus().equals("0")){
            whereSql.append("and ee.ee_status='"+queryParams.getStatus()+"' and " )
        }*/
//        int firstIndex = (queryParams.getCurPage() - 1) * queryParams.getPageSize();
//        int lastIndex=queryParams.getCurPage() * queryParams.getPageSize();
//        int lastIndex = queryParams.getPageSize();
        // sql.append(whereSql).append(orderSql).toString();
        sql.append(whereSql).append(orderSql).toString();
//        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SeismicHazardAssessQueryParams.class)).size();
        List<SeismicHazardAssessQueryParams> seismicHazardAssessQueryParams = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SeismicHazardAssessQueryParams.class));
        for (SeismicHazardAssessQueryParams Param : seismicHazardAssessQueryParams) {
            String ywrys=Param.getIaZddwYwry()+","+Param.getIaSsdwYwry()+","+Param.getIaXtdwYwry();
            Param.setYwrys(ywrys);
            if ("地震危险性评估".equals(Param.getContent())){
                Param.setSign(Param.getIaNumber()+"@"+"1");
            }else {
                Param.setSign(Param.getIaNumber()+"@"+"0");
            }
        }
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", seismicHazardAssessQueryParams);
        return map;
    }

}
